<?php
namespace Admin\Controller;
use \Admin\Controller\IndexController;

/**
*@author denny <denny@mobnt>
*商品控制器
 */
class GoodsController extends IndexController 
{
    //添加商品
    public function add()
    {
        if(IS_POST){
            $model = D('Admin/Goods');
            if($model->create(I('post.'), 1)){
                if($id = $model->add()){
                    $this->success('添加成功！', U('list?p='.I('get.p')));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        $this->setPageBtn('添加商品', '商品列表', U('list?p='.I('get.p')));
        $this->display();
    }

    //修改商品
    public function edit()
    {
        $id = I('get.id');
        if(IS_POST){
            $model = D('Admin/Goods');
            if($model->create(I('post.'), 2)){
                if($model->save() !== FALSE){
                    $this->success('修改成功！', U('list', array('p' => I('get.p', 1))));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('Goods');
        $data = $model->find($id);
        $this->assign('data', $data);

        $this->setPageBtn('修改商品', '商品列表', U('list?p='.I('get.p')));
        $this->display();
    }

    //删除商品
    public function delete()
    {
        $model = D('Admin/Goods');
        if($model->delete(I('get.id', 0)) !== FALSE){
            $this->success('删除成功！', U('list', array('p' => I('get.p', 1))));
            exit;
        }
        else {
            $this->error($model->getError());
        }
    }

    //商品列表
    public function index()
    {
        $model = D('Admin/Goods');
        $data = $model->search();
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));

        $this->setPageBtn('商品列表', '添加商品', U('add'));
        $this->display();
    }
}